package com.qf.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsBrandMapper;
import com.qf.pojo.DtsBrand;
import com.qf.pojo.DtsCategory;
import com.qf.util.AliyunOSSUtil;
import com.qf.vo.CatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author Wy
 * @version V1.0
 * @Project MavenDemo
 * @Package com.qf.service
 * @Description:
 * @Date 2022/6/21 11:14
 */
@Service
public class brandServiceImpl implements  brandService {
    @Autowired
    private DtsBrandMapper mapper;

    //阿里云北京服务器地址
    @Value("${aliyun.oss.endpoint}")
    private String endpoint;

    //子账户账号
    @Value("${aliyun.oss.accessKeyId}")
    private String accessKeyId;

    //子账户密码
    @Value("${aliyun.oss.accessKeySecret}")
    private String accessKeySecret;
    //桶名字
    @Value("${aliyun.oss.bucketName}")
    private  String bucketName;
    @Override
    public IPage<DtsBrand> list(Integer id, String name, Integer page, Integer limit, String sort, String order) {
        //创建分页对象
        IPage<DtsBrand> page1 = new Page<>(page,limit);
        //创建查询对象
        QueryWrapper<DtsBrand> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("deleted", false);
        if(!StringUtils.isEmpty(id)){
            objectQueryWrapper.eq("id", id);
        }
        if(!StringUtils.isEmpty(name)){
            objectQueryWrapper.like("name", name);
        }

        if(!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)){
            if(order.equalsIgnoreCase("asc")){
                objectQueryWrapper.orderByAsc();
            }else {
                objectQueryWrapper.orderByDesc();
            }
        }
        IPage<DtsBrand> dtsBrandIPage = mapper.selectPage(page1, objectQueryWrapper);

        return  dtsBrandIPage;
    }

    /**
     *
     * @return
     */
    @Override
    public List<CatVo> list() {
        List<CatVo> list = mapper.list();
        return list;
    }


    /**
     * 添加
     * @param brand
     * @return
     */
    @Override
    public DtsBrand add(DtsBrand brand) {
        brand.setAddTime(new Date());
        brand.setUpdateTime(new Date());
        brand.setDeleted(false);
        mapper.insert(brand);
        return brand;
    }

    /**
     * 修改
     * @param brand
     * @return
     */
    @Override
    public int update(DtsBrand brand) {
        brand.setUpdateTime(new Date());
        int i = mapper.updateById(brand);
        return i;
    }

    /**
     * 删除
     * @param brand
     * @return
     */
    @Override
    public int deleted(DtsBrand brand) {
        String picUrl = brand.getPicUrl();
        if(!StringUtils.isEmpty(picUrl)){
            int i = picUrl.lastIndexOf("/");
            int i1 = picUrl.lastIndexOf("?");
            String substring = picUrl.substring(i + 1, i1);
            //删除oos
            AliyunOSSUtil.deleteFile(endpoint, accessKeyId, accessKeySecret, bucketName, substring);
        }

        //2. 根据key删除数据库中的数据
        Integer id = brand.getId();
        QueryWrapper<DtsBrand> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("id", id);
        int delete = mapper.delete(objectQueryWrapper);
        return delete;

    }


}
